﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.驾驶员管理
{
    public partial class DriverInfoList : System.Web.UI.Page
    {
        TruckTeamBLL teamBLL = new TruckTeamBLL();
        DriverBLL dirveBLL = new DriverBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                team_list();
                dirverinfo();
            }
        }

        #region 车队绑定
        private void team_list()
        {
            DataTable dt = teamBLL.GetTruckList();
            drop_truckteam.DataSource = dt;
            drop_truckteam.DataTextField = "TeamName";
            drop_truckteam.DataValueField = "TeamID";
            drop_truckteam.DataBind();
            drop_truckteam.Items.Insert(0, new ListItem("未绑定", "0"));
        }
        #endregion

        #region 显示驾驶员信息
        private void dirverinfo()
        {
            //获取输入的条件信息 
            string name = txt_name.Text.Trim();
            int sex = Convert.ToInt32(rlist_sex.SelectedValue);
            string idcard = txt_number.Text.Trim();
            string phone = txt_phone.Text.Trim();
            //生日
            DateTime? birth_in = null;
            if (!string.IsNullOrWhiteSpace(dateinput.Value.Trim()))
            {
                birth_in = Convert.ToDateTime(dateinput.Value);
            }
            DateTime? birth_end = null;
            if (!string.IsNullOrWhiteSpace(txt_minder.Text.Trim()))
            {
                birth_end = Convert.ToDateTime(txt_minder.Text);
            }
            //聘用日期
            DateTime? checktime_in = null;
            if (!string.IsNullOrWhiteSpace(dateinput.Value.Trim()))
            {
                checktime_in = Convert.ToDateTime(dateinput.Value);
            }
            DateTime? checktime_end = null;
            if (!string.IsNullOrWhiteSpace(txt_zhi.Text.Trim()))
            {
                checktime_end = Convert.ToDateTime(txt_zhi.Text);
            }
            DriverMODEL model = new DriverMODEL()
            {
                Name = name,
                Sex = sex,
                IDCard = idcard,
                Brith_in = birth_in,
                Brith_end = birth_end,
                Phone = phone,
                CheckInTime_in = checktime_in,
                CheckInTime_end = checktime_end
            };
            List<DriverQueryModel> list = dirveBLL.Getdrivrmodel(model);
            griv_Driver.DataSource = list;
            griv_Driver.DataBind();
        }
        #endregion

        #region 检索按钮
        /// <summary>
        /// 检索按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_jiansuo_Click(object sender, EventArgs e)
        {
            dirverinfo();
        }
        #endregion

        #region 删除按钮
        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void griv_Driver_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                //获取选中的用户ID
                int driverid = Convert.ToInt32(e.CommandArgument);

                int result = dirveBLL.Getdrive_delete(driverid);
                if (result == 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！') </script>");
                    dirverinfo();
                }
                else if (result == -1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('用户已删除,请勿重复删除！') </script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败') </script>");
                }
            }
        }
        #endregion

        #region 重置按钮
        /// <summary>
        /// 重置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_chongzhi_Click(object sender, EventArgs e)
        {
            txt_name.Text = null;
            rlist_sex.SelectedValue = "0";
            txt_number.Text = "";
            dateinput.Value = "";
            txt_minder.Text = "";
            rblist_state.SelectedValue = "0";
            drop_truckteam.SelectedValue = "0";
            dateinput.Value = "";
            txt_zhi.Text = "";
        }
        #endregion

        #region 添加按钮
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            //跳转添加驾驶员信息页面
            Response.Redirect("AddDriver.aspx");
        } 
        #endregion
    }
}